package cn.cdeden.knowledge.controller.enterpriseMaterialFilesTree;


import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.knowledge.service.EnterpriseMaterialFilesTreeService;
import cn.cdeden.knowledge.service.dto.EnterpriseMaterialFilesTreeDto;
import cn.cdeden.knowledge.service.dto.EnterpriseMaterialFilesTreeQueryParam;
import cn.cdeden.knowledge.service.dto.Upload;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author GW
* @date 2024-02-21
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/enterpriseMaterialFilesTree")
public class EnterpriseMaterialFilesTreeController {

    private final EnterpriseMaterialFilesTreeService enterpriseMaterialFilesTreeService;

    @GetMapping
    public TableDataInfo query(EnterpriseMaterialFilesTreeQueryParam query, Pageable pageable){
        TableDataInfo<EnterpriseMaterialFilesTreeDto> info = enterpriseMaterialFilesTreeService.queryAll(query,pageable);
        return info;
    }

    @PostMapping
    public R create(@ModelAttribute EnterpriseMaterialFilesTreeDto resources){
        return R.ok(enterpriseMaterialFilesTreeService.insert(resources));
    }

    @PutMapping
    public R update(@Validated @RequestBody EnterpriseMaterialFilesTreeDto resources){
        return R.ok(enterpriseMaterialFilesTreeService.updateById(resources));
    }

    @DeleteMapping
    public R delete(@RequestBody Set<Long> ids) {
        return R.ok(enterpriseMaterialFilesTreeService.deleteByIds(ids));
    }

    /*
    @Log("导出插件--企业资源库")
    @ApiOperation("导出插件--企业资源库")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('enterpriseMaterialFilesTree:list')")
    public void download(HttpServletResponse response, EnterpriseMaterialFilesTreeQueryParam query) throws IOException {
        enterpriseMaterialFilesTreeService.download(enterpriseMaterialFilesTreeService.queryAll(query), response);
    }*/

    /**
     * 上传资源库
     * @param upload
     * @return
     */
    @PostMapping("/uploadMaterial")
    public R uploadMaterial(@ModelAttribute Upload upload){
        return R.ok(enterpriseMaterialFilesTreeService.uploadEnterpriseMaterialFiles(upload.getImage(), upload.getFile(),upload.getRealName() ,upload.getParentId(),upload.getDeptId()));
    }

    /**
     * 查询资源列表
     */
    @GetMapping("/getMaterialPage")
    public TableDataInfo getMaterialPage(EnterpriseMaterialFilesTreeQueryParam query, Pageable pageable){
        TableDataInfo<EnterpriseMaterialFilesTreeDto> info = enterpriseMaterialFilesTreeService.getMaterialsFilePage(query,pageable);
        return info;
    }
}
